<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<% String path=pageContext.getServletContext().getContextPath(); %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>中药处方</title>
<link rel="stylesheet" type="text/css" href="<%=path %>/eazyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=path %>/eazyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="<%=path %>/eazyui/demo/demo.css">
	<script type="text/javascript" src="<%=path %>/eazyui/jquery.min.js"></script>
	<script type="text/javascript" src="<%=path %>/eazyui/jquery.easyui.min.js"></script>	
	
	<link href="<%=path %>/bootstrap/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
	<link href="<%=path %>/bootstrap/bootstrap-3.3.7-dist/css/bashboard.css" rel="stylesheet">
	<link href="<%=path %>/bootstrap/bootstrap-validator/dist/css/bootstrapValidator.min.css" rel="stylesheet">
	
  	<script type="text/javascript" src="<%=path %>/bootstrap/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  	<script type="text/javascript" src="<%=path %>/bootstrap/bootstrap-validator/dist/js/bootstrapValidator.min.js"></script>

	<style type="text/css">
		a{ color:#999; text-decoration:none;}
		a:hover{ text-decoration:underline;}
		#menu li{
			padding:8px 20px;
			list-style:none;
			border-bottom:1px solid #AEEEEE;
		}
	</style>
	<script>
	
	function formatterBtn(value,row,index){
		var str ='<a href="#" name="editbtn" class="easyui-linkbutton" title="修改角色信息" onclick="edityizhu(&quot;'+row.fck_id+'&quot;,'+index+');">医嘱</a> &nbsp;&nbsp;'
		  +'<a href="#" name="removebtn" class="easyui-linkbutton" title="删除角色信息" onclick="deleteCM(&quot;'+row.fck_id+'&quot;,'+index+');" >删除</a>';
		return str;
			
	}
	function onLoadSuccess(){
		
		$("a[name='editbtn']").linkbutton({
			plain:true,
		    iconCls: 'icon-edit'
		});
		$("a[name='removebtn']").linkbutton({
			plain:true,
		    iconCls: 'icon-remove'
		});
	}
	function addformatterBtn(value,row,index){
		var str ='<a href="#" name="editbtn" class="easyui-linkbutton" title="添加" onclick="addCM(&quot;'+row.ii_id+'&quot;,&quot;'+row.ii_name+'&quot;,&quot;'+row.ii_dept+'&quot;,&quot;'+row.ii_price+'&quot;,'+index+');">选择</a> &nbsp;&nbsp;';
		return str;
			
	}
	function edityizhu(ck_id,index){
		$("#edit").window("open");
		var guahao_id =$("#guahao_id").text();
		if(cm_id!=""){
			$.get("<%=path %>/getCKYizhu",{ck_id:ck_id,guahao_id:guahao_id},function(data,textStatus){
				
						$("#editck_id").textbox("setValue",ck_id);
						$("#editck_yizhu").textbox("setValue",data.ck_yizhu);			
						$("#editIndex").val(index);	
			})				
		}
	}
function saveyizhu(){
		var guahao_id =$("#guahao_id").text();
		var ck_id = $("#editck_id").val();
		var ck_yizhu = $("#editck_yizhu").val();
		var index =$("#editIndex").val();
		alert(index);
		$.post("<%=path %>/updateCKYizhu",{ck_id:ck_id,guahao_id:guahao_id,ck_yizhu:ck_yizhu},function(data,textStatus){
			
			
			if(data==true){
				alert("修改成功");
				
				$('#edit').window("close");
				$('#dg1').datagrid('updateRow',{
					index:index,
					row:{
						fck_yizhu:ck_yizhu,
					}
			});
			onLoadSuccess();
			$('#edit').window("close");
		}
		
	
	})
	}
	function deleteCM(fcm_id,index){
		var guahao_id =$("#guahao_id").text();
		$.ajax({
			url:"<%=path %>/deleteCK",
			data:{guahao_id:guahao_id,ck_id:fck_id},
			/*dataType:"json",*/
			success:function(data,textStatus){
				if(textStatus=="success"){
					
					if(data==true){
						alert("删除成功");
						
						$('#dg1').datagrid('deleteRow',index);
						var rows = $('#dg1').datagrid("getRows");
						$("#dg1").datagrid("loadData",rows);
					}
				}
			},
			error:function(data,textStatus){
				alert("error:"+textStatus);
			},
		})
	}
function addCM(ii_id,ii_name,ii_dosage,ii_price,index){
		var guahao_id =$("#guahao_id").text();
		var ii_num=$("#"+index).val();
		var ii_amount=ii_price*ii_num;
		$.ajax({
			url:"<%=path %>/insertCK",
			data:{guahao_id:guahao_id,ii_id:ii_id,ii_num:ii_num},
			/*dataType:"json",*/
			success:function(data,textStatus){
				if(textStatus=="success"){
					
					if(data==true){
						alert("添加成功");
						
						$('#dg1').datagrid('appendRow',{
							fck_id:ii_id,
							fck_name:ii_name,
							fck_usage:ii_dosage,
							fck_price:ii_price,
							fck_num:ii_num,
							fck_amount:ii_amount,
							fck_yizhu:"",
						});
						onLoadSuccess();
						$('#add').window("close");
					}
				}
			},
			error:function(data,textStatus){
				alert("error:"+textStatus);
			},
		})
		$("#"+index).val(null);
	}
	function SearchCM(){
		var cm_name=$("#searchcm_name").val();
		var pageSize = $("#pg").pagination('options').pageSize;
		var pageNumber= $("#pg").pagination('options').pageNumber;
		$.ajax({url:"<%=path %>/searchCK",
			data:{ii_name:ii_name,pageNumber:pageNumber,pageSize:pageSize},
			/*dataType:"json",*/
			success:function(data,textStatus){
				if(textStatus=="success"){
					
					console.log(data);
					$("#dg2").datagrid("loadData",data.list);
					$("#pg").pagination('refresh',{total:data.total});
				}
			},
			error:function(data,textStatus){
				alert("error:"+textStatus);
			},
		})
	
	}
	
	function onSelectPage(pageNumber,pageSize){
		var cm_name=$("#searchcm_name").val();
		/*var pageSize = $("#dg1").pagination.pageSize;*/
		$.ajax({url:"<%=path %>/searchCM",
			data:{ii_name:ii_name,pageNumber:pageNumber,pageSize:pageSize},
			/*dataType:"json",*/
			success:function(data,textStatus){
				if(textStatus=="success"){
					
					console.log(data);
					$("#dg2").datagrid("loadData",{'total':data.total,rows:data.list});
				}
			},
			error:function(data,textStatus){
				alert("error:"+textStatus);
			},
		})
	
	}
	</script>
</head>
<body>


	<div id="toolbar" style="background:green;margin:5px 5px 5px 0;">
		
		<a href="<%=path %>/WM?guahao_id=${register.guahao_id}" class="easyui-linkbutton"  plain="true"  style="background:white;border:1px solid;width:24%;">西医处方</a>
		<a href="<%=path %>/TCM?guahao_id=${register.guahao_id}" class="easyui-linkbutton"  plain="true"  style="background:white;border:1px solid;width:24%;">中医处方</a>
		<a href="<%=path %>/CK?guahao_id=${register.guahao_id}" class="easyui-linkbutton"  plain="true"  style="background:white;border:1px solid;width:24%;">检查项目</a>
		<a href="javascript:void(0)" class="easyui-linkbutton"  plain="true"  style="background:white;border:1px solid;width:24%;">处方列表</a>
	</div>
	<div style="display: none;">
	<p>挂号ID：<span id="guahao_id">${register.guahao_id }</span></p>
	</div>
	<div id="patient" style="margin:0;">
			<table id="dg0" class="easyui-datagrid" style="width: 100%" >
    		<thead>
				<tr>
					<th field="patient_name" width="15%">姓名：</th>
					<th field="patient_age" width="15%">年龄：</th>
					<th field="patient_sex" width="15%">性别：</th>
					<th field="patient_idcard" width="25%">身份证号：</th>					
					<th field="dept_id" width="15%">科室：</th>
					<th field="doctor_id" width="15%">医生：</th>
				</tr>				
    		</thead>	
    		<tbody>
    			<tr>
    				<td>${patient.patient_name }</td>
    				<td>${patient.patient_age }</td>
    				<td>
    				   <c:if test="${patient.patient_sex ==0}">女</c:if>
    				   <c:if test="${patient.patient_sex ==1}">男</c:if>
    				</td>
    				<td>${patient.patient_idcard }</td>
    				<td>${register.dept_id }</td>
    				<td>${register.doctor_id }</td>
    			</tr>
    		
    		</tbody>
    	</table>
    </div>
	        
	<div style="padding:20px 0;">
		<a href="#" class="easyui-linkbutton" title="选择药品"  data-options="iconCls:'icon-add'" onclick="$('#add').window('open')">选择药品</a>
	</div>
   
		<div id="content" style="margin:0 10px 0 0;">
			<table id="dg1" class="easyui-datagrid" style="width: 100%" data-options="onLoadSuccess:onLoadSuccess">
    		<thead>
				<tr>
					<th field="fck_id" width="10%">检查号</th>
					<th field="fck_name" width="10%">检查名称</th>
					<th field="fck_dept" width="10%">科室</th>
					<th field="fck_price" width="10%">单价</th>
					
					<th field="fck_num" width="10%">数量</th>
					<th field="fck_amount" width="10%">金额</th>
					<th field="fck_yizhu" width="20%">医嘱</th>

					<th data-options="field:'action',formatter:formatterBtn" width="20%">操作</th>
				</tr>
				
    		</thead>						  
			<tbody>
				
					<c:forEach items="${ck_prescription }" var="ck">
					<tr>
						<td>${ck.ck_id }</td>
						<td>${ck.ck_name }</td>
						<td>${ck.ck_dept }</td>
						<td>${ck.ck_price}</td>
						<td>${ck.ck_num }</td>
						<td>${ck.ck_amount }</td>
						<td>${ck.ck_yizhu }</td>
					</tr>	
					</c:forEach>			
													 									 
			</tbody>	
        </table>
              	    

	</div>
	<div id="add" class="easyui-window" title="选择药品" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1000px;height:500px;padding:10px;">
		<div style="margin:0 auto;width:80%;">
			<div style="margin:5px auto;">
				<div>药品名称：</div>
				<input class="easyui-textbox" id="searchcm_name" style="width:25%;height:25px;"> 
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" style="width:25%;height:25px;" onclick="SearchCM();">搜索</a>
				
			</div>
			<table id="dg2" class="easyui-datagrid" style="width: 100%" data-options="onLoadSuccess:onLoadSuccess">
    		<thead>
				<tr>
					<th field="ii_id" width="15%">检查号</th>
					<th field="ii_name" width="20%">检查名称</th>
					<th field="ii_dept" width="10%">科室</th>					
					<th field="ii_price" width="10%">单价</th>					
					<th field="ii_num" width="15%">数量</th>
					<th data-options="field:'action',formatter:addformatterBtn" width="30%">操作</th>
				</tr>
				
    		</thead>						  
			<tbody>
				<c:forEach items="${page.list}" var="ii" varStatus="id">
				<tr>
					<td>${ii.ii_id }</td>
					<td>${ii.ii_name }</td>
					<td>${ii.ii_dept }</td>
					<td>${ii.ii_price }</td>
					<td><input id='${id.index }'></td> 					
				</tr>
				</c:forEach>									 									 
			</tbody>
			
        </table>
        <div id="pg" class="easyui-pagination" data-options="total:${page.total},onSelectPage:onSelectPage"></div>		
		</div>
</div>
<div id="edit" class="easyui-window" title="选择药品" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1000px;height:500px;padding:10px;">
		<div style="margin:0 auto;width:80%;">
			<div style="margin:5px auto;">
				<input id="editIndex" type="hidden">
				<div>检查id:</div>
				<input id="editck_id" readonly="readonly" class="easyui-textbox">
				<div>医嘱:</div>
				<input class="easyui-textbox" id="editck_yizhu" style="width:100%;height:25px">
				<div style="margin-top:10px">
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" style="width:100%;height:32px" onclick="saveyizhu();">保存</a>
			</div>
			</div>
		</div>
</div>
 
		  

					 	   
</body>
</html>